﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FollowMe.Model;
using System.Data.Entity;
using FollowMe.Repository;

namespace FollowMe.Test
{

    class FakeFMContext : IContext
    {
        public IDbSet<Friend> friends { get; set; }
        public IDbSet<Location> locations { get; set; }
        public IDbSet<Message> messages { get; set; }
        public IDbSet<Member> members { get; set; }
        public IDbSet<RegistrationID> registrationIDs { get; set; }

        public FakeFMContext()
        {
            friends = new FakeFriendSet();
            locations = new FakeLocationSet();
            members = new FakeMemberSet();
            registrationIDs = new FakeRegistrationSet();
            messages = new FakeMessageSet();
        }

        public IDbSet<TEntity> getSet<TEntity>() where TEntity : class
        {
            if (typeof(TEntity) == typeof(Friend))
            {
                return (IDbSet<TEntity>)friends;
            }
            if (typeof(TEntity) == typeof(Member))
            {
                return (IDbSet<TEntity>)members;
            }
            if (typeof(TEntity) == typeof(Location))
            {
                return (IDbSet<TEntity>)locations;
            }
            if (typeof(TEntity) == typeof(RegistrationID))
            {
                return (IDbSet<TEntity>)registrationIDs;
            }
            if (typeof(TEntity) == typeof(Message))
            {
                return (IDbSet<TEntity>)messages;
            }

            return null;
        }

        public void markDeleted<TEntity>(TEntity entity) where TEntity : class
        {
            //do nothing
        }

        public void markModified<TEntity>(TEntity entity) where TEntity : class
        {
            //do nothing
        }

        public Boolean isDetached<TEntity>(TEntity entity) where TEntity : class
        {
            return true;
        }

        public int SaveChanges() {
            return 0;
        }
    }


    public class FakeFriendSet : FakeDbSet<Friend>
    {
        public override Friend Find(params object[] keyValues)
        {
            return this.SingleOrDefault(e => e.FriendAddedID == (Guid)keyValues.Single() || e.MemberAddingID == (Guid)keyValues.Single());
        }
    }
    public class FakeMemberSet : FakeDbSet<Member>
    {
        public override Member Find(params object[] keyValues)
        {
            return this.SingleOrDefault(e => e.MemberId == (Guid)keyValues.Single());
        }
    }
    public class FakeMessageSet : FakeDbSet<Message>
    {
        public override Message Find(params object[] keyValues)
        {
            return this.SingleOrDefault(e => e.MessageId == (Guid)keyValues.Single());
        }
    }
    public class FakeRegistrationSet : FakeDbSet<RegistrationID>
    {
        public override RegistrationID Find(params object[] keyValues)
        {
            return this.SingleOrDefault(e => e.ID == (Guid)keyValues.Single());
        }
    }
    public class FakeLocationSet : FakeDbSet<Location>
    {
        public override Location Find(params object[] keyValues)
        {
            return this.SingleOrDefault(e => e.LocationID == (Guid)keyValues.Single());
        }
    }
    
}
